<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import {getCount} from "@/api/dashboard/chart";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      let _this = this;
      getCount().then((response) => {
        _this.chart.setOption({
            title: {
              text: '职级分布',
              left: 'center',
              top: -4,
              textStyle: {
                color: '#06a7f3'
              }
            },
            xAxis: {
              type: 'category',
              data: ['正高级', '副高级', '中级', '助理级', '员级', '无职称', '初级']
            },
            yAxis: {
              type: 'value'
            },
            series: [
              {
                data: response.data.numbers,
                type: 'line',
                smooth: true
              }
            ]
          })
        });
    },

  }
}
</script>
